<?php
namespace backend\controllers;

use backend\models\Banner;
use common\components\BaseController;
use Yii;
use \common\helps\tools;
use \common\helps\code;
use \common\models\UploadForm;
/**
 * Banner controller
 */
class BannerController extends BaseController
{

    public $modelClass = 'backend\models\Banner';
    public function init()
    {
        parent::init();
    }

    /**
     * 获取banner列表
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionGetBannerList(){
        $request =$this->request;
        $model = new Banner();
        $params = $request->queryParams;
        $total_data = $model->getBannerList($params);
        $data = tools::getPageData($total_data,empty($params['page_size'])?'':$params['page_size'],empty($params['page'])?'':$params['page']);

        if(!empty($params['excel_download']) && $params['excel_download']>0){
            $tableHeader = ['id','标题','跳转链接','模块分类','模块分类id','状态','排序'];
            foreach($total_data as &$value){
                if($value['status']>0){
                    $value['status'] ='启用';
                }else{
                    $value['status'] ='停用';
                }
                unset($value['pic']);
                unset($value['info']);
            }
            tools::ToPHPEXECL($tableHeader,$total_data,'后台banner列表');
        }


        return code::success($data);

    }


    /**
     * 停用banner
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionUpdateBannerState(){
        if($this->request->isPost) {
            $request =$this->request;
            $model = new Banner();
            $params = $request->bodyParams;
            if(!empty($params['type']) && $params['type']==1){ //编辑停启用
                $model->scenario='update_status';
                $model->load($params,'');
                if(!$model->validate()){
                    $error = array_values($model->getErrors());
                    return code::error('',$error[0][0]);
                }
                $data=$model->editBannerState($params);
                if(!empty($data['error'])){
                    return code::error('',$data['error']);
                }else{
                    return code::success();
                }
            }else{
                return code::error('','参数错误');
            }

        }
    }

    /**
     * 删除banner
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionDelBannerState(){
        if($this->request->isPost) {
            $request =$this->request;
            $model = new Banner();
            $params = $request->bodyParams;
            if(!empty($params['type']) && $params['type']==2){ //删除
                $model->scenario='update_status';
                $model->load($params,'');
                if(!$model->validate()){
                    $error = array_values($model->getErrors());
                    return code::error('',$error[0][0]);
                }
                $data=$model->editBannerState($params);
                if(!empty($data['error'])){
                    return code::error('',$data['error']);
                }else{
                    return code::success();
                }
            }else{
                return code::error('','参数错误');
            }

        }
    }



    /**
     * 编辑Banner
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionUpdateBannerInfo(){
        if($this->request->isPost) {
            $request =$this->request;
            $model = new Banner();
            $params = $request->bodyParams;
            if(!empty($params['type']) && $params['type']==2){ //编辑
                $model->load($params,'');
                if(!$model->validate()){
                    $error = array_values($model->getErrors());
                    return code::error('',$error[0][0]);
                }
                $data=$model->editBannerInfo($params);
                if(!empty($data['error'])){
                    return code::error('',$data['error']);
                }else{
                    return code::success();
                }
            }else{
                return code::error('','操作类型参数错误');
            }

        }
    }


    /**
     * 新增Banner
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionAddBannerInfo(){
        if($this->request->isPost) {
            $request =$this->request;
            $model = new Banner();
            $params = $request->bodyParams;
            if(!empty($params['type']) && $params['type']==1){ //新增
                $model->load($params,'');
                if(!$model->validate()){
                    $error = array_values($model->getErrors());
                    return code::error('',$error[0][0]);
                }
                $data=$model->editBannerInfo($params);
                if(!empty($data['error'])){
                    return code::error('',$data['error']);
                }else{
                    return code::success();
                }
            }else{
                return code::error('','参数错误');
            }

        }
    }


    /**
     * 上传banner图
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public function actionBannerUpload()
    {
        $model = new UploadForm();
        if (Yii::$app->request->isPost) {
            $files = $_FILES['file'];
            if(empty($files)){
                return code::error('','未获取到文件对象');
            }
            $model->file =$files;
            $dir=UPLOADS.'user/';
            $res = $model->upload($dir,$files);
            if($res){
                return code::success(array('data'=>array('file_name'=>$res)),'上传成功');
            }else{
                return code::error('','上传失败');
            }
        }

        return code::error('','操作错误');
    }


    /**
     * 获取banner分类列表
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionGetBannerClassList(){
        $model = new Banner();
        $total_data = $model->getBannerClassList();

        return code::success($total_data);

    }

}
